package com.example.mvvmcore.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.example.mvvmcore.viewmodel.BaseViewModel;
import com.example.mvvmcore.viewmodel.MyViewModelFactory;

public abstract class BaseMVVMFragment<VM extends BaseViewModel> extends BaseFragment{
    protected VM mViewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MyViewModelFactory factory=new MyViewModelFactory(getBaseActivity());
        mViewModel= (VM) new ViewModelProvider(requireActivity(),factory).get(getViewModelClazz());//createViewModel();
        initData();
    }


    protected abstract void initData();

    protected abstract Class getViewModelClazz();
}
